package com.vader.flea.service.impl;

import com.vader.flea.dao.ShopDao;
import com.vader.flea.dto.ImgHolder;
import com.vader.flea.dto.ShopExecution;
import com.vader.flea.entity.Shop;
import com.vader.flea.enums.ShopStateEnum;
import com.vader.flea.exceptions.ShopOperationException;
import com.vader.flea.service.ShopService;
import com.vader.flea.util.ImgUtil;
import com.vader.flea.util.PageCalculator;
import com.vader.flea.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopDao shopDao;


    @Override
    public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
        int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
        List<Shop> shopList = shopDao.queryShopList(shopCondition, rowIndex, pageSize);
        int count = shopDao.queryShopCount(shopCondition);
        ShopExecution shopExecution = new ShopExecution();
        if (shopList != null) {
            shopExecution.setShopList(shopList);
            shopExecution.setCount(count);
        } else {
            shopExecution.setState(ShopStateEnum.INNER_ERROR.getState());
        }
        return shopExecution;
    }


    @Override
    @Transactional
    public ShopExecution addShop(Shop shop, ImgHolder thumbnail) throws ShopOperationException {
        if (shop == null) {
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }
        try {
            shop.setEnableStatus(0);
            shop.setCreateTime(new Date());
            shop.setUpdateTime(new Date());
            int rows = shopDao.insertShop(shop);
            if (rows <= 0) {
                throw new ShopOperationException("店铺创建失败");
            } else {
                if (thumbnail.getImage() != null) {
                    try {
                        addShopImg(shop, thumbnail);
                    } catch (Exception e) {
                        throw new ShopOperationException("addShopImg error 店铺创建失败 " + e.getMessage());
                    }
                    rows = shopDao.updateShop(shop);
                    if (rows <= 0) {
                        throw new ShopOperationException("更新图片地址失败");
                    }
                }
            }
        } catch (Exception e) {
            throw new ShopOperationException("addShop error" + e.getMessage());
        }
        return new ShopExecution(ShopStateEnum.CHECK, shop);
    }

    public Shop getShopById(Long shopId) {
        Shop shop = shopDao.queryByShopId(shopId);
        return shop;
    }

    @Override
    public ShopExecution modifyShop(Shop shop,ImgHolder thumbnail) throws ShopOperationException {
        try {
            if (shop == null || shop.getShopId() == null) {
                return new ShopExecution(ShopStateEnum.NULL_SHOP);
            } else {

                if (thumbnail.getImage() != null && thumbnail.getImage() != null && !" ".equals(thumbnail.getImgName())) {
                    Shop tempShop = shopDao.queryByShopId(shop.getShopId());
                    if (tempShop.getShopImg() != null) {
                        ImgUtil.deleteFileOrPath(tempShop.getShopImg());
                    }
                    addShopImg(shop, thumbnail);
                }
                shop.setUpdateTime(new Date());
                int rows = shopDao.updateShop(shop);
                if (rows <= 0) {
                    return new ShopExecution(ShopStateEnum.INNER_ERROR);
                } else {
                    shop = shopDao.queryByShopId(shop.getShopId());
                    return new ShopExecution(ShopStateEnum.SUCCESS, shop);
                }
            }
        } catch (Exception e) {
            throw new ShopOperationException("modifyShop Error" + e.getMessage());
        }
    }

    private void addShopImg(Shop shop, ImgHolder thumbnail) {
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = ImgUtil.generateThumbnail(thumbnail, dest);
        shop.setShopImg(shopImgAddr);
    }
}
